﻿@*@using Xms.Abp.Data
@using AntDesign.TableModels
@typeparam TAppService
@typeparam TDetailsResult
@typeparam TPagingListResult
@typeparam TExportResult
@typeparam TImportParameter
@typeparam TKey
@typeparam TPagedParameter
@typeparam TCreateParameter
@typeparam TUpdateParameter
@typeparam TListViewModel
@typeparam TDetailsViewModel
@typeparam TCreateViewModel
@typeparam TUpdateViewModel
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase


@{
    Dictionary<string, int> gutter = new()
    {
        ["xs"] = 8,
        ["sm"] = 16,
        ["md"] = 24,
        ["lg"] = 32,
        ["xl"] = 48,
        ["xxl"] = 64
    };
}



<Row Gutter="@gutter">
    <Col Span="12">
        @if (Creatable && HasCreatePermission)
        {
        <Button Icon="plus" OnClick="OnCreateClickAsync">创建</Button>
        }
        @if (Exportable && HasExportPermission)
        {
        <Button Icon="export" OnClick="OnExportClickAsync">导出数据</Button>
        }
        @if (DeleteManyable || Importable || (ToolbarMoreActions != null && ToolbarMoreActions.Length > 0))
        {
            <DropdownButton>
                <Overlay>
                    <Menu>
                        @if (DeleteManyable && HasDeleteManyPermission)
                        {
                        <MenuItem OnClick="OnDeleteManyClickAsync">批量删除</MenuItem>
                        }
                        @if (Importable && HasImportPermission)
                        {
                        <MenuItem OnClick="OnDownloadTemplateClickAsync">下载导入模板</MenuItem>
                        <MenuItem OnClick="OnImportClickAsync">导入数据</MenuItem>
                        }
                        @if(ToolbarMoreActions != null && ToolbarMoreActions.Length > 0)
                        {
                        foreach (var toolbarAction in ToolbarMoreActions)
                        {
                        <MenuItem OnClick="() => OnToolbarMoreActionClickAsync(toolbarAction)">@toolbarAction</MenuItem>
                        }
                        }
                    </Menu>
                </Overlay>
                <Unbound>
                    批量操作
                </Unbound>
            </DropdownButton>
        }
    </Col>
    <Col Span="12">
        <Input TValue="string" @bind-Value="PagedParameter.Filter" Placeholder="模糊搜索内容" />
        <Button Type="@ButtonType.Primary" Icon="search" OnClick="OnSearchClickAsync">搜索</Button>
        <Button Type="@ButtonType.Link" Icon="search" OnClick="OnAdvanceSearchClickAsync">高级搜索</Button>
    </Col>
</Row>
<div>

</div>
<Row Gutter="@gutter">
    <Col Span="12">

    </Col>
    <Col Span="12">

    </Col>
</Row>
<div>
    <Table @ref="TableRef"
        TItem="TListViewModel"
        @bind-Total="TotalCount"
        @bind-PageIndex="CurrentPageIndex"
        @bind-PageSize="CurrentPageSize"
        Bordered="true"
        Size="TableSize.Small"
        RowExpandable="IsTableRowExpandable()"
        DataSource="CurrentDataSource"
        Loading="PagingLoading"
        OnChange="OnPagingChange"
        RemoteDataSource>
        <RowTemplate>
        @if (AllColumnsTempate != null)
        {
            @AllColumnsTempate
        }
        else
        {
            <Selection Key="@(context.Id.ToString())" Disabled="IsSelectionDisabled(context)" Fixed="left" />
            @if (ColumnsTempate != null)
            {
                @ColumnsTempate
            }
            else
            {
                foreach(var column in GetTableColumnInfos())
                {
                <Column TData="object" Fixed="@column.Fixed" Align="@column.Align" Format="@column.Format" DataIndex="@column.Name" Sortable="@column.Sortable" SorterMultiple="@column.SorterMultiple">
                    <TitleTemplate>
                        <span>@if (!column.Icon.IsNullOrWhiteSpace()) { <Icon Type="@column.Icon" /> }@column.Title</span>
                    </TitleTemplate>
                    <ChildContent>
                    @if (column.Editable && context is IEditingInRow e && (e.IsCreating || e.IsEditing))
                    {
                        @if (column.Property.PropertyType.IsEnum)
                        {
                            <Select TItem="string" TItemValue="object" Placeholder="Please select" AllowClear Value="@column.GetValue(context)">
                                @foreach(var item in Enum.GetValues(column.Property.PropertyType))
                                {
                                <SelectOption TItem="string" TItemValue="object" Value="@item">@item.ToString()</SelectOption>
                                }
                            </Select>
                        }
                        else if(column.Property.PropertyType == typeof(bool))
                        {
                            var sp = column.SwitchText?.Split('|') ?? new[] { "", "" };
                            <Switch Checked="@(column.GetValue(context).ToLower() == "true")" CheckedChildren="@sp[0]" UnCheckedChildren="@sp[1]" />
                        }
                        else
                        {
                            <Input TValue="object" Value="@column.GetValue(context)" OnChange="v => column.SetValue(EditingEntity, v)" />
                        }
                    }
                    else
                    {
                        @if (column.Property.PropertyType.IsEnum)
                        {
                            @column.GetValue(context)
                        }
                        else if(column.Property.PropertyType == typeof(bool))
                        {
                            @if (context is IHasEnabled s)
                            {
                            var sp = column.SwitchText?.Split('|') ?? new[] { "Enabled", "Disabled" };
                            var isSaving = context is IEditingInRow ed && ed.IsSaving;
                            <Switch Checked="@(column.GetValue(context).ToLower() == "true")" CheckedChildren="@sp[0]" UnCheckedChildren="@sp[1]" CheckedChanged="en => OnEnabledChangedAsync(context, en)" @bind-Disabled="@(!HasToggleEnablePermission || isSaving)" @bind-Loading="isSaving" />
                            }
                            else
                            {
                            var sp = column.SwitchText?.Split('|') ?? new[] { "Open", "Close" };
                            @(column.GetValue(context).ToLower() == "true" ? sp[0] : sp[1])
                            }
                        }
                        else
                        {
                        @column.GetValue(context)
                        }
                    }
                    </ChildContent>
                </Column>
                }
            }
            <ActionColumn Title="Action" Fixed="right">
                <Space>
                @if (context is IEditingInRow e && (e.IsCreating || e.IsEditing))
                {
                    if (e.IsCreating)
                    {
                    <SpaceItem><Button Type="@ButtonType.Primary" OnClick="()=>OnSaveCreateClickAsync(context)" @bind-Loading="e.IsSaving" @bind-Disabled="e.IsSaving">Save</Button></SpaceItem>
                    <SpaceItem>
                        <Popconfirm Title="Sure to cancel?" OnConfirm="() => OnCancelCreateClickAsync(context)" OkText="Yes" CancelText="No">
                            <Button>Cancel</Button>
                        </Popconfirm>
                    </SpaceItem>
                    }
                    else
                    {
                    <SpaceItem><Button Type="@ButtonType.Primary" OnClick="()=>OnSaveEditClickAsync(context)" @bind-Loading="e.IsSaving" @bind-Disabled="e.IsSaving">Save</Button></SpaceItem>
                    <SpaceItem>
                        <Popconfirm Title="Sure to cancel?" OnConfirm="() => OnCancelEditClickAsync(context)" OkText="Yes" CancelText="No">
                            <Button>Cancel</Button>
                        </Popconfirm>
                    </SpaceItem>
                    }
                }
                else
                {
                    <SpaceItem><Button OnClick="()=>OnDetailsClickAsync(context)">Details</Button></SpaceItem>
                    <SpaceItem><Button Type="@ButtonType.Primary" OnClick="()=>OnEditClickAsync(context)">Edit</Button></SpaceItem>
                    <SpaceItem><Button Danger OnClick="()=>OnDeleteClickAsync(context)">Delete</Button></SpaceItem>
                    @if (RowMoreActions != null && RowMoreActions.Length > 0)
                    {
                        <DropdownButton>
                            <Overlay>
                                <Menu>
                                @foreach (var rowAction in RowMoreActions)
                                {
                                    <MenuItem OnClick="()=>OnRowMoreActionClickAsync(rowAction, context)">@rowAction</MenuItem>
                                }
                                </Menu>
                            </Overlay>
                            <Unbound Context="ctx">
                                More actions
                            </Unbound>
                        </DropdownButton>
                    }
                }
                </Space>
            </ActionColumn>
        }
        </RowTemplate>
        <ExpandTemplate>
        @if (context is IHasRemark s && !s.Remark.IsNullOrWhiteSpace())
        {
            @s.Remark
        }
        </ExpandTemplate>
        <PaginationTemplate>
            <Pagination
                @ref="PaginationRef"
                ShowTotal="PagingShowTotal()"
                DefaultCurrent="1"
                @bind-Total="TotalCount"
                @bind-Current="CurrentPageIndex"
                @bind-PageSize="CurrentPageSize"
                TotalBoundaryShowSizeChanger="10"
                OnChange="OnPagingChange"
                Size="small"
                ShowQuickJumper
                ShowSizeChanger />
        </PaginationTemplate>
    </Table>
</div>
*@